<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

?>
<?php
class Admin_Form_Movie extends Zend_Form{
	private $_movie;
	private $_channels;

	public function __construct( Default_Model_Movie $movie, $channels = array(), $options = null ){
		parent::__construct( $options );

		$this->_movie 		= $movie;
		$this->_channels	= $channels;

		$this->addElement( 'text', 'title', array(
			'value'			=> $movie->title,
			'required'		=> true,
			'label'			=> 'title:',
			'validators'	=>	array(array('StringLength', 254, 1 ))
		));

		$this->addElement( 'text', 'creator', array(
			'value'			=> $movie->creator,
			'required'		=> true,
			'label'			=> 'creator:',
			'validators'	=>	array(array('StringLength', 254, 1 ))
		));

		$this->addElement( 'text', 'publisher', array(
			'value'			=> $movie->publisher,
			'required'		=> true,
			'label'			=> 'publisher:',
			'validators'	=>	array(array('StringLength', 254, 1 ))
		));

		$this->addElement( 'text', 'contributor', array(
			'value'			=> $movie->contributor,
			'required'		=> true,
			'label'			=> 'contributors:',
			'validators'	=>	array(array('StringLength', 254, 1 ))
		));

		$this->addElement( 'text', 'language', array(
			'value'			=> $movie->language,
			'required'		=> true,
			'label'			=> 'laguage:',
			'validators'	=>	array(array('StringLength', 64 ))
		));

//        $form->addDisplayGroup(array('username', 'password'), 'login');

		$this->addElement( 'textarea', 'subject', array(
			'value'			=> $movie->subject,
			'required'		=> true,
			'label'			=> 'subject:',
			'validators'	=>	array(array('StringLength', 512, 1 ))
		));

		$this->addElement( 'textarea', 'description', array(
			'value'			=> $movie->description,
			'required'		=> true,
			'label'			=> 'description:',
			'validators'	=>	array(array('StringLength', 1024, 1 ))
		));

		$this->addElement( 'textarea', 'rights', array(
			'value'			=> $movie->rights,
			'required'		=> true,
			'label'			=> 'copyright:',
			'validators'	=>	array(array('StringLength', 254, 1 ))
		));

		$info = $movie->getInfo();

		$this->addElement('text', 'poster_frame', array(
			'value'			=> intval($movie->posterFrame),
			'required'		=> true,
			'label'			=> 'posterframe',
			'validators'	=> array(array('between', null, array(1, $info->duration)))
		));

        $this->addDisplayGroup( array_keys( $this->getElements() ), 'metadata', array(
            'legend' => 'Metadata'
        ));


		$this->addSubForm( $this->getDateForm(), 'date');
		$this->addSubForm( $this->getChannelForm(), 'channels');

		$this->addElement( 'submit', 'Save', array(
			'label'	=> 'Save'
		));
	}

	private function getDateForm(){
		list($y,$m,$d,$h,$m,$s) = preg_split('/\D/', $this->_movie->date );

		$date 		= new Zend_Form_SubForm();
		$timestamp 	= strtotime('0');

		$years  = range(1900, date('Y'));
		$days   = range(1, 31);
		$months = range(1, 12);

		$date->setLegend( 'date' );


		foreach( range(1, 12) as $i ){
			$months[$i] = strftime('%B', strtotime(sprintf('+%d Months', $i)));
		}
		unset( $months[0] );


		$date->addElement( 'select', 'month', array(
			'value'	=> $m,
			'multiOptions'	=> $months
		));

		$date->addElement( 'select', 'day', array(
			'value'	=> $d,
			'multiOptions'	=> array_combine( range(1, 31), range(1,31) )
		));

		$date->addElement( 'select', 'year', array(
			'value'	=> $y,
			'multiOptions'	=> array_combine($years,$years)
		));

		foreach( $date->getElements() as $element ){
			$element->removeDecorator('HtmlTag');
			$element->removeDecorator('Label');
		}

		$date->removeDecorator('HtmlTag');
		$date->setAttrib( 'class', 'date-select' );

		return $date;
	}

	private function getChannelForm(){
		$form = new Zend_Form_SubForm();
		$form->setLegend('channels');

		$channels = $this->_movie->getChannels();
		$selected = array_map( create_function('$value', 'return $value->id;'), $channels);

		$options = array();
		foreach( $this->_channels as $channel ){
			$options[$channel->id] = $channel->name;
		}

		$form->addElement('MultiCheckbox', 'selected', array(
			'multiOptions'	=> $options,
			'value'	=> $selected
		));

		foreach( $form->getElements() as $element ){
			$element->removeDecorator('HtmlTag');
			$element->removeDecorator('Label');
		}

		$form->removeDecorator('HtmlTag');
		return $form;
	}
}
